/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.hycreate.platform.modules.crm.calendar;

import java.io.IOException;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hycreate.platform.common.config.Global;
import com.hycreate.platform.common.utils.StringUtils;
import com.hycreate.platform.common.web.BaseController;
import com.hycreate.platform.modules.crm.business.Customer;
import com.hycreate.platform.modules.crm.business.CustomerService;
import com.hycreate.platform.modules.crm.business.HycrmContact;
import com.hycreate.platform.modules.crm.business.HycrmContactService;
import com.hycreate.platform.modules.crm.project.ProjectService;
import com.hycreate.platform.modules.sys.entity.Office;
import com.hycreate.platform.modules.sys.entity.User;
import com.hycreate.platform.modules.sys.utils.UserUtils;

/**
 * 日程的管理Controller
 * @author ly
 * @version 2016-12-03
 */
@Controller
@RequestMapping(value = "${adminPath}/crm/calendar/hycrmCalendar")
public class HycrmCalendarController extends BaseController {

	@Autowired
	private HycrmCalendarService hycrmCalendarService;
	@Autowired
	private CustomerService customerService;
	@Autowired
	private HycrmContactService hycrmContactService;
	@Autowired
	private ProjectService projectService;
	
	@ModelAttribute
	public HycrmCalendar get(@RequestParam(required=false) String id) {
		HycrmCalendar entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = hycrmCalendarService.get(id);
		}
		if (entity == null){
			entity = new HycrmCalendar();
		}
		return entity;
	}
	
	
	@RequiresPermissions("calendar:hycrmCalendar:view")
	@RequestMapping(value = "calendarView")
	public String calendarView(Model model) throws ParseException{
		HycrmCalendar hycrmCalendar = new HycrmCalendar();
		hycrmCalendar.setOperateMan(UserUtils.getUser().getId());
		//日程管理
		List<HycrmCalendar> hycrmCalendars = hycrmCalendarService.findList(hycrmCalendar);
		ObjectMapper mapper = new ObjectMapper();
		String json = "";
		try {
			json = mapper.writeValueAsString(hycrmCalendars);
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		}
		List<Customer> customers = customerService.findList(null);
		//部门
		List<Office> dept = projectService.findAllDept();
		
		model.addAttribute("json",json);
		model.addAttribute("dept",dept);
		model.addAttribute("customers",customers);
		return "modules/crm/calendar/myCalendarPage";
	}

	@RequiresPermissions("calendar:hycrmCalendar:edit")
	@RequestMapping(value = "save")
	public void save(HycrmCalendar hycrmCalendar, Model model, RedirectAttributes redirectAttributes,HttpServletRequest request, HttpServletResponse response) {
		/*if (!beanValidator(model, hycrmCalendar)){
			return form(hycrmCalendar, model);
		}*/
		hycrmCalendarService.save(hycrmCalendar);
		addMessage(redirectAttributes, "保存日程成功");
		response.setContentType("application/text;charset=utf-8");
		try {
			response.getWriter().print("true");
		} catch (IOException e) {
			e.printStackTrace();
		}
		//return "redirect:"+Global.getAdminPath()+"/calendar/hycrmCalendar/calendarView";
	}
	
	@RequiresPermissions("calendar:hycrmCalendar:edit")
	@RequestMapping(value = "delete")
	public String delete(HycrmCalendar hycrmCalendar, RedirectAttributes redirectAttributes) {
		hycrmCalendarService.delete(hycrmCalendar);
		addMessage(redirectAttributes, "删除日程成功");
		return "redirect:"+Global.getAdminPath()+"/crm/calendar/hycrmCalendar/calendarView";
	}
	
	//根据时间分配日程--查找有时间的用户
	@RequestMapping(value = "selectdate")
	public void selectdate(@RequestParam("selectdate") Date selectdate,@RequestParam("deptId") String deptId,HttpServletResponse response){
		List<User> userUnused = hycrmCalendarService.findUserUnused(selectdate,deptId);
		ObjectMapper om = new ObjectMapper();
		try {
			String json = om.writeValueAsString(userUnused);
			response.setContentType("application/json;charset=utf-8");
			response.getWriter().print(json);
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@RequiresPermissions("calendar:hycrmCalendar:view")
	@RequestMapping(value = "customerContact")
	public void customerContact(@RequestParam("custId") String custId,HttpServletResponse response){
		HycrmContact hycrmContact = new HycrmContact();
		hycrmContact.setCustomerId(custId);
		List<HycrmContact> conts = hycrmContactService.findList(hycrmContact);
		
		ObjectMapper om = new ObjectMapper();
		try {
			String json = om.writeValueAsString(conts);
			response.setContentType("application/json;charset=utf-8");
			response.getWriter().print(json);
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	//select id where id not in(select id from *****)customerContact
}